package com.elai.tags.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.tags.domain.BaseTagsTypeDO;
import com.elai.tags.service.BaseTagsTypeService;
import com.elai.common.utils.poi.ExcelUtil;

/**
 * 标签类型Controller
 *
 * @author elai
 * @date 2021-06-27
 */
@RestController
@RequestMapping("/tag/type")
public class BaseTagsTypeController extends BaseController
{
    @Autowired
    private BaseTagsTypeService baseTagsTypeService;

    /**
     * 查询标签类型列表
     */
    @GetMapping("/list")
    public AjaxResult list(BaseTagsTypeDO baseTagsType)
    {
        List<BaseTagsTypeDO> list = baseTagsTypeService.selectBaseTagsTypeList(baseTagsType);
        return AjaxResult.success(list);
    }

    /**
     * 导出标签类型列表
     */
    @Log(title = "标签类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseTagsTypeDO baseTagsType)
    {
        List<BaseTagsTypeDO> list = baseTagsTypeService.selectBaseTagsTypeList(baseTagsType);
        ExcelUtil<BaseTagsTypeDO> util = new ExcelUtil<BaseTagsTypeDO>(BaseTagsTypeDO.class);
        return util.exportExcel(list, "type");
    }

    @GetMapping("/gettagsTreeData")
    public AjaxResult gettagsTreeData()
    {
        Map<String,Object> result = baseTagsTypeService.selectBaseTagsTypeTreeData();
        return AjaxResult.success(result);
    }

    @GetMapping("/getTagsParent")
    public AjaxResult getTagsParent()
    {
        BaseTagsTypeDO type=new BaseTagsTypeDO();
        type.setParentId(0L);
        List<BaseTagsTypeDO> parentList = baseTagsTypeService.selectBaseTagsTypeList(type);
        Map map=new HashMap();
        map.put("id",0L);
        map.put("type","type");
        map.put("label","标签");
        List<Map<String,Object>> childList=new ArrayList<>();
        parentList.stream().forEach(c->{
            Map<String,Object> child=new HashMap<>();
            child.put("id",c.getId());
            child.put("type","type");
            child.put("label",c.getTypeName());
            childList.add(child);
        });
        map.put("children",childList);
        return AjaxResult.success(map);
    }

    /**
     * 获取标签类型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseTagsTypeService.selectBaseTagsTypeById(id));
    }

    /**
     * 新增标签类型
     */
    @Log(title = "标签类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseTagsTypeDO baseTagsType)
    {
        return toAjax(baseTagsTypeService.insertBaseTagsType(baseTagsType));
    }

    /**
     * 修改标签类型
     */
    @Log(title = "标签类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseTagsTypeDO baseTagsType)
    {
        return toAjax(baseTagsTypeService.updateBaseTagsType(baseTagsType));
    }

    /**
     * 删除标签类型
     */
    @Log(title = "标签类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseTagsTypeService.deleteBaseTagsTypeByIds(ids));
    }
}
